import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import {
  Form,
  Input,
  Modal,
  Select,
  message,
} from 'antd';
import { modalLayoutSm } from '@/config/layout';
import { request } from 'umi';
const DictModal = (props: any, refs: any) => {
  const [form] = Form.useForm();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;

  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);
  const { TextArea } = Input;
  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    props.callback && props.callback();
  };
  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        Obj = {
          ...Obj,
        };

        const url = code ? '/sys/group/' : '/sys/group/';
        const method = code ? 'PUT' : 'POST';
        if (code) {
          Obj.groupId = code;
        } else {
        }
        request(url, {
          method: method,
          headers: {
            'Content-Type': 'application/json',
          },
          data: Obj,
        }).then((res) => {
          message.success(res?.meta?.msg);
          setIsModalVisible(false);
          handleCancel();
        });
      })
      .catch((info) => { });
  };

  const getDetail = (id: any) => {
    request('/sys/group/' + id, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        id,
      },
    }).then((res) => {
      setFieldsValue(res?.data);
      showModal();
    });
  };
  useImperativeHandle(refs, () => ({
    init: (id: any, record: any) => {
      if (id) {
        setCode(id);
        getDetail(id);
      } else {
        resetFields();
        showModal();
      }

      // setfileList([]);
    },
  }));

  return (
    <Modal
      title={code === '' ? '新增' : '编辑'}
      visible={isModalVisible}
      onOk={handleOk}
      onCancel={handleCancel}
      centered
      width={1000}
      maskClosable={false}
      bodyStyle={{
        maxHeight: window.innerHeight - 150,
        overflowY: 'auto',
      }}
    >
      <Form form={form} layout="vertical" {...modalLayoutSm}>
        <Form.Item
          label="卡片名称"
          name="title"
          rules={[{ required: true, message: '请输入卡片名称' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="卡片适用设备端"
          name="sourceType"
          rules={[{ required: true, message: '请选择设备端!' }]}
        >
          <Select>
            <Option value='WEB'>WEB</Option>
            <Option value='APP'>APP</Option>
          </Select>
        </Form.Item>
        <Form.Item
          label="排序"
          name="sno"
        >
          <Input />
        </Form.Item>
        <Form.Item label="描述" name="remark">
          <TextArea rows={4} />
        </Form.Item>
        {/* <Form.Item label="描述" name="remark">

        </Form.Item> */}
      </Form>
    </Modal>
  );
};
const AddMCom = forwardRef(DictModal);
export default AddMCom;
